package com.zhiyizhen.handheld.component.pharmacy;

import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentTransaction;

import com.zhiyizhen.handheld.R;
import com.zhiyizhen.handheld.common.DataCache;
import com.zhiyizhen.handheld.common.ResourcesUtil;
import com.zhiyizhen.handheld.common.constant.AppConstant;
import com.zhiyizhen.handheld.common.view.BaseFragment;
import com.zhiyizhen.handheld.component.main.bean.MenuItemBean;
import com.zhiyizhen.handheld.databinding.FragmentPharmacyBinding;

import java.util.List;

/**
 * 药房Tab
 */
public class PharmacyFragment extends BaseFragment {
    private FragmentPharmacyBinding binding;
    private RegimenDosageFragment regimenDosageFragment;
    private GiveDosageFragment giveDosageFragment;
    private FragmentTransaction fragmentTransaction;
    private final static int regimenDosageFragmentIndex = 1;
    private final static int giveDosageFragmentIndex = 2;
    private int currentIndex = 0;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentPharmacyBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initTabLayout();
        setMenu();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
//            setMenu();
        }
    }

    private void setMenu() {
        binding.dosageRegimenLayout.setVisibility(View.GONE);
        binding.giveDosageLayout.setVisibility(View.GONE);
        List<MenuItemBean> menuList = DataCache.getMenuList();
        fragmentTransaction = getChildFragmentManager().beginTransaction();
        for (int i = 0; i < menuList.size(); i++) {
            if (TextUtils.equals(menuList.get(i).getPerms(), AppConstant.MENU_PHARMACY)) {
                List<MenuItemBean> childrenList = menuList.get(i).getChildren();
                if (childrenList == null || childrenList.isEmpty()) {
                    return;
                }
                for (int j = 0; j < childrenList.size(); j++) {
                    // 配药
                    if (TextUtils.equals(childrenList.get(j).getPerms(), AppConstant.MENU_PHARMACY_PY)) {
                        binding.dosageRegimenLayout.setVisibility(View.VISIBLE);
                        regimenDosageFragment = new RegimenDosageFragment();
                        fragmentTransaction.add(R.id.pharmacy_fragment_container, regimenDosageFragment);
                    } else if (TextUtils.equals(childrenList.get(j).getPerms(), AppConstant.MENU_PHARMACY_FY)) {
                        binding.giveDosageLayout.setVisibility(View.VISIBLE);
                        giveDosageFragment = new GiveDosageFragment();
                        fragmentTransaction.add(R.id.pharmacy_fragment_container, giveDosageFragment);
                    }
                }
            }
        }
        if (binding.dosageRegimenLayout.getVisibility() == View.VISIBLE) {
            hideFragments(fragmentTransaction, regimenDosageFragmentIndex);
            showRegimenDosageFragment(fragmentTransaction);
        } else if (binding.giveDosageLayout.getVisibility() == View.VISIBLE) {
            hideFragments(fragmentTransaction, giveDosageFragmentIndex);
            showGiveDosageFragment(fragmentTransaction);
        }
    }

    private void initTabLayout() {
//        fragmentTransaction = getChildFragmentManager().beginTransaction();
//        regimenDosageFragment = new RegimenDosageFragment();
//        fragmentTransaction.add(R.id.pharmacy_fragment_container, regimenDosageFragment);
//        giveDosageFragment = new GiveDosageFragment();
//        fragmentTransaction.add(R.id.pharmacy_fragment_container, giveDosageFragment);
//        hideFragments(fragmentTransaction, regimenDosageFragmentIndex);
//        showRegimenDosageFragment(fragmentTransaction);

        binding.giveDosageTv.setOnClickListener(view -> {
            if (currentIndex == giveDosageFragmentIndex) {
                return;
            }
            fragmentTransaction = getChildFragmentManager().beginTransaction();
            hideFragments(fragmentTransaction, giveDosageFragmentIndex);
            showGiveDosageFragment(fragmentTransaction);
        });
        binding.dosageRegimenTv.setOnClickListener(view -> {
            if (currentIndex == regimenDosageFragmentIndex) {
                return;
            }
            fragmentTransaction = getChildFragmentManager().beginTransaction();
            hideFragments(fragmentTransaction, regimenDosageFragmentIndex);
            showRegimenDosageFragment(fragmentTransaction);
        });
    }

    private void hideFragments(FragmentTransaction fragmentTransaction, int selectIndex) {
        if (selectIndex == regimenDosageFragmentIndex) {
            if (giveDosageFragment != null) {
                fragmentTransaction.hide(giveDosageFragment);
            }
        } else if (selectIndex == giveDosageFragmentIndex) {
            if (regimenDosageFragment != null) {
                fragmentTransaction.hide(regimenDosageFragment);
            }
        }
    }

    private void showGiveDosageFragment(FragmentTransaction fragmentTransaction) {
        currentIndex = giveDosageFragmentIndex;
        fragmentTransaction.show(giveDosageFragment);
        fragmentTransaction.commit();

        binding.giveDosageTv.setTextColor(ResourcesUtil.getColor(R.color.colorPrimary1));
        binding.giveDosageTv.setTextSize(18);
        binding.giveDosageTv.setTypeface(binding.giveDosageTv.getTypeface(), Typeface.BOLD);
        binding.dosageRegimenTv.setTextColor(ResourcesUtil.getColor(R.color.black_66));
        binding.dosageRegimenTv.setTypeface(binding.dosageRegimenTv.getTypeface(), Typeface.NORMAL);
        binding.dosageRegimenTv.setTextSize(16);

        binding.belowGiveDosageTv.setVisibility(View.VISIBLE);
        binding.belowdosageRegimenTv.setVisibility(View.GONE);
    }

    private void showRegimenDosageFragment(FragmentTransaction fragmentTransaction) {
        currentIndex = regimenDosageFragmentIndex;
        fragmentTransaction.show(regimenDosageFragment);
        fragmentTransaction.commit();

        binding.giveDosageTv.setTextColor(ResourcesUtil.getColor(R.color.black_66));
        binding.giveDosageTv.setTextSize(16);
        binding.giveDosageTv.setTypeface(binding.giveDosageTv.getTypeface(), Typeface.NORMAL);
        binding.dosageRegimenTv.setTextColor(ResourcesUtil.getColor(R.color.colorPrimary1));
        binding.dosageRegimenTv.setTypeface(binding.dosageRegimenTv.getTypeface(), Typeface.BOLD);
        binding.dosageRegimenTv.setTextSize(18);

        binding.belowdosageRegimenTv.setVisibility(View.VISIBLE);
        binding.belowGiveDosageTv.setVisibility(View.GONE);
    }
}



